import { Scene } from '@babylonjs/core/scene';
import { AbstractControl, IAbstractControlOptions } from './abstract-control';
import { Control } from '@babylonjs/gui/2D/controls/control';
import { TextBlock, TextWrapping } from '@babylonjs/gui/2D/controls/textBlock';
import { ScrollViewer } from '@babylonjs/gui/2D/controls/scrollViewers/scrollViewer';
import { ScrollBar } from '@babylonjs/gui/2D/controls/sliders/scrollBar';
import { ImageScrollBar } from '@babylonjs/gui/2D/controls/sliders/imageScrollBar';


/**
 * 文字块滚动窗控件可选参数
 * @param text 内容
 * @param fontColor 字体颜色
 * @param fontSize 字体大小
 * @param fontFamily 字体
 * @param fontStyle 字体样式
 * @param fontWeight 字体权重
 * @param lineSpacing 行间距
 * @param horizontalAlignment 内容水平布局
 * @param verticalAlignment 内容垂直布局
 * @param textWrapping 如何断行
 * @param margin 外边区域厚度
 * @param marginColor 外边区域颜色
 * @param backgroundColor 内容区域背景颜色
 * @param cornerRadius 圆角半径
 * @param barSize 滚动条大小
 */
interface IUITextScrollOptions extends IAbstractControlOptions {
    text?: string,
    fontColor?: string,
    fontSize?: number,
    fontFamily?: string,
    fontStyle?: string,
    fontWeight?: string,
    lineSpacing?: number,
    horizontalAlignment?: number,
    verticalAlignment?: number,
    textWrapping?: boolean | TextWrapping,
    margin?: number,
    marginColor?: string,
    backgroundColor?: string,
    cornerRadius?: number,
    barSize?: number,
}


/**
 * 文字块滚动窗控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 */
class UITextScroll extends AbstractControl {

    private _textControl!: TextBlock;
    private _verticalControl!: ScrollBar | ImageScrollBar;

    constructor(name: string, scene: Scene, options?: IUITextScrollOptions) {
        super(name, scene, options);
    }

    public set text(txt: string) {
        (this.options as IUITextScrollOptions).text = txt;
        this.createTextControl(this.control as ScrollViewer);
    }

    public get text(): string {
        return this._textControl.text;
    }

    public set fontColor(color: string) {
        this._textControl.color = color;
    }

    public get fontColor(): string {
        return this._textControl.color;
    }

    public set fontSize(value: number) {
        this._textControl.fontSize = value;
    }

    public get fontSize(): number {
        return this._textControl.fontSize as number;
    }

    public get barSize(): number {
        return (this.control as ScrollViewer).barSize;
    }

    public set barSize(value: number) {
        (this.control as ScrollViewer).barSize = value;
    }

    protected initializeControl(name: string, scene: Scene, options: IUITextScrollOptions): Control {
        const sv = new ScrollViewer(name + '-control-scroll');
        sv.thickness = options.margin === undefined ? 15 : options.margin;
        sv.color = options.marginColor || '#000000';
        sv.background = options.backgroundColor || '#000000';
        sv.cornerRadius = options.cornerRadius === undefined ? 5 : options.cornerRadius;
        sv.barSize = options.barSize === undefined ? 25 : options.barSize;
        this._verticalControl = sv.verticalBar;

        this.createTextControl(sv);
        return sv;
    }

    private createTextControl(ctl: ScrollViewer): void {
        const options = this.options as IUITextScrollOptions;
        if (this._textControl) {
            ctl.removeControl(this._textControl);
            this._textControl.dispose();
        }

        const text = new TextBlock(this.name + '-control-text');
        text.color = options.fontColor || '#ffffff';
        text.fontSize = options.fontSize || 26;
        text.fontFamily = options.fontFamily || 'Arial';
        text.fontStyle = options.fontStyle || 'normal';
        text.fontWeight = options.fontWeight || 'normal';
        text.lineSpacing = options.lineSpacing || 10;
        text.verticalAlignment = text.textVerticalAlignment = options.verticalAlignment === undefined ? Control.VERTICAL_ALIGNMENT_TOP : options.verticalAlignment;
        text.horizontalAlignment = text.textHorizontalAlignment = options.horizontalAlignment === undefined ? Control.HORIZONTAL_ALIGNMENT_LEFT : options.horizontalAlignment;
        text.textWrapping = options.textWrapping === undefined ? 4 : options.textWrapping;
        text.text = options.text || 'text';

        this._textControl = text;
        ctl.addControl(text);
        text.resizeToFit = true;
    }

    protected bindEvent(control: Control, options: IAbstractControlOptions): void {
        this._verticalControl.onWheelObservable.add(this.breakCamera);
        this._verticalControl.onPointerDownObservable.add(this.breakCamera);
        control.onWheelObservable.add(this.breakCamera);
    }

}


/**
 * 创建文字块控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 * @returns 文字块控件
 */
const CreateUITextScroll = (name: string, scene: Scene, options?: IUITextScrollOptions) => {
    return new UITextScroll(name, scene, options);
};


export { UITextScroll, CreateUITextScroll };
export type { IUITextScrollOptions };